package com.knowswift.issues.util;

import com.knowswift.common.bean.project.po.MonitorSetting;
import com.knowswift.common.bean.project.vo.MonitorSettingVO;
import com.knowswift.common.common.GlobalProperty;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class MonitorUtil {

    public static boolean isInRange(Integer value, Integer min, Integer max) {
        //没设置区间的时 为true
        if (value == null) {
            return true;
        }
        boolean result = true;
        if (min != null) { // [min, +∞)
            result = value >= min;
        }
        if (max != null) { // (-∞, max]
            result = result && (value <= max);
        }
        // if min and max are not null, [min, max]
        return result;
    }

    public static MonitorSettingVO getSetting(List<MonitorSetting> list) {
        Map<String, MonitorSetting> temp = list.stream().collect(Collectors.toMap(MonitorSetting::getName, a -> a));
        MonitorSettingVO monitorSettingVO = new MonitorSettingVO();
        MonitorSetting temperature = temp.get(GlobalProperty.SETTING_NAME_TEMPERATURE);
        if (temperature != null) {
            monitorSettingVO.setTemperatureMin(temperature.getMin());
            monitorSettingVO.setTemperatureMax(temperature.getMax());
            monitorSettingVO.setTemperatureEnable(temperature.getEnable());
            monitorSettingVO.setTemperatureInvocation(temperature.getInvocation());
        }

        MonitorSetting humidity = temp.get(GlobalProperty.SETTING_NAME_HUMIDITY);
        if (humidity != null) {
            monitorSettingVO.setHumidityMin(humidity.getMin());
            monitorSettingVO.setHumidityMax(humidity.getMax());
            monitorSettingVO.setHumidityEnable(humidity.getEnable());
           monitorSettingVO.setHumidityInvocation(humidity.getInvocation());
        }

        MonitorSetting electric = temp.get(GlobalProperty.SETTING_NAME_ELECTRICITY);
        if (electric != null) {
            monitorSettingVO.setElectricityMin(electric.getMin());
            monitorSettingVO.setElectricityMax(electric.getMax());
            monitorSettingVO.setElectricityEnable(electric.getEnable());
            monitorSettingVO.setElectricityInvocation(electric.getInvocation());
        }
        return monitorSettingVO;
    }

}
